package com.guigu.pd.base.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.entity.*;
import com.guigu.pd.base.service.*;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.basefeign.FleetFeign;
import com.guigu.pd.serviceapi.basefeign.TripsTruckDriverFeign;
import com.guigu.pd.serviceapi.basefeign.TruckLicenseFeign;
import com.guigu.pd.serviceapi.pojo.TruckLicenseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.base.dao.TruckDao;


@Service("truckService")
public class TruckServiceImpl extends ServiceImpl<TruckDao, TruckEntity> implements TruckService {


    @Autowired
    private TruckTypeService truckTypeService;

    @Autowired
    private FleetService fleetService;

    @Autowired
    TruckLicenseFeign truckLicenseFeign;

    @Autowired
    TransportTripsTruckDriverService transportTripsTruckDriverService;

    @Autowired
    TransportTripsService transportTripsService;

    @Autowired
    TransportLineService transportLineService;

    @Autowired
    CoreOrgFeign coreOrgFeign;

    @Autowired
    FleetFeign fleetFeign;

    @Autowired
    TripsTruckDriverFeign tripsTruckDriverFeign;

    @Override
    public PageVo<TruckEntity> queryTruckfy(Map<String, Object> params) {

        TruckEntity
                truckEntity = JSON.parseObject(JSON.toJSONString(params),
                TruckEntity.class);

        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        QueryWrapper<TruckEntity> queryWrapper = new QueryWrapper<TruckEntity>();

        //根据
        if (StringUtils.isNotBlank(truckEntity.getFleetId())) {
            queryWrapper.like("fleet_id", truckEntity.getFleetId());
        }
        if (StringUtils.isNotBlank(truckEntity.getTruckTypeId())) {
            queryWrapper.like("truck_type_id", truckEntity.getTruckTypeId());
        }
        if (StringUtils.isNotBlank(truckEntity.getLicensePlate())) {
            queryWrapper.like("license_plate", truckEntity.getLicensePlate());
        }

        PageHelper.startPage(pageno, pagesize);
        List<TruckEntity> list = this.list(queryWrapper);


        for (TruckEntity entity : list) {
            //车辆类型名称
            TruckTypeEntity byId = truckTypeService.getById(entity.getTruckTypeId());
            if (byId!=null) {
                entity.setTruckTypename(byId.getName());
            }
            //获取车队名称
            FleetEntity byId1 = fleetService.getById(entity.getFleetId());
            entity.setFleetname(byId1.getName());

            List<String> tripslist = tripsTruckDriverFeign.gettripsidbytruckId(entity.getId());
            String tid = "";
            for (int i = 0; i < tripslist.size(); i++) {
                if (i>0) {
                    tid = tid + "," + tripslist.get(i);
                }else {
                    tid = tid + tripslist.get(i);
                }
            }
            entity.setTripsid(tid);

        }
        PageInfo<TruckEntity> pageInfo = new PageInfo<TruckEntity>(list);

        PageVo<TruckEntity> pageVo = new PageVo<TruckEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public TruckEntity gettruckInfobyId(String id) {
        TruckEntity truck = this.getById(id);
        TruckLicenseEntity truckLicense = truckLicenseFeign.getbytruckId(truck.getId());
        truck.setTruckLicense(truckLicense);

        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();
        queryWrapper.eq("truck_id", truck.getId());
        List<TransportTripsTruckDriverEntity> trucktripslist = transportTripsTruckDriverService.list(queryWrapper);

        String agencyname = "";
        for (TransportTripsTruckDriverEntity trucktrips : trucktripslist) {
            QueryWrapper<TransportTripsEntity> tripsEntityQueryWrapper = new QueryWrapper<TransportTripsEntity>();
            tripsEntityQueryWrapper.eq("id",trucktrips.getTransportTripsId());
            TransportTripsEntity trips = transportTripsService.getOne(tripsEntityQueryWrapper);
            if (trips!=null){
                QueryWrapper<TransportLineEntity> transportLineQueryWrapper = new QueryWrapper<TransportLineEntity>();
                transportLineQueryWrapper.eq("id", trips.getTransportLineId());
                TransportLineEntity transportLine = transportLineService.getOne(transportLineQueryWrapper);

                agencyname = agencyname + coreOrgFeign.getcornamebyid(transportLine.getAgencyId());

                truck.setTripsname(trips.getName());
                truck.setTripstruckNumber(transportLine.getName());
            }
        }





        String fleetname = fleetFeign.getfleetnamebyId(truck.getFleetId());

        truck.setAgencyname(agencyname);
        truck.setFleetname(fleetname);

        return truck;
    }
}